#!/bin/bash
function sys_optimize() {
    echo "系统优化" >>$logs_file
    whiptail --backtitle "Author: Elvis" --title "$prompt_message" --yesno "${sysOptimize_prompt[0]}：\
    \n1.${sysOptimize_prompt[1]}\n2.${sysOptimize_prompt[2]}\n3.${sysOptimize_prompt[3]}\n4.${sysOptimize_prompt[4]}\n5.${sysOptimize_prompt[5]}\
    \n6.${sysOptimize_prompt[6]}\n7.${sysOptimize_prompt[7]}\n8.${sysOptimize_prompt[8]}\
    \n\n${sysOptimize_prompt[9]}？" 18 60 --yes-button "$goon_button" --no-button "$return_button"
    if [ "$?" != "0" ];then
        return 1
    fi
    {
    ####修改内核
    #进度条输出格式echo -e "XXX\n进度数字\n输出内容\nXXX"
    msg="${sysOptimize_process[0]}"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n1\n$msg\nXXX"
    echo 1
    sleep 1
    all_para="net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_timestamps = 1
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.somaxconn = 1024
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_mem = 196608 262144 393216
net.ipv4.tcp_max_syn_backlog = 4096
net.core.netdev_max_backlog = 2500
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_synack_retries = 3
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.ip_local_port_range = 1024 65535
net.nf_conntrack_max = 1048576
net.ipv4.tcp_fin_timeout = 40
fs.file-max = 655350
fs.inotify.max_user_instances = 8192
fs.inotify.max_user_watches = 50000000
fs.inotify.max_queued_events = 327679
vm.min_free_kbytes = 65536
vm.swappiness = 0"
    #删除之前存在的配置
    all_para_keys=${all_para// /}
    for key in $all_para_keys
    do
      para_key=$(echo $key|awk -F"=" '{print $1}')
      sed -i "/$para_key/d" /etc/sysctl.conf
    done
    
    #添加新的配置
    echo 10
    sleep 1
    cat >> /etc/sysctl.conf <<EOF
 $all_para
EOF
    modprobe ip_conntrack
    echo "`date "+%Y-%m-%d %H:%M:%S"` ${sysOptimize_process[1]}" >>$logs_file
    sysctl -p >>$logs_file 2>&1

    ####修改limits####
    msg="${sysOptimize_process[2]}"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n20\n$msg\nXXX"
    echo 20
    sleep 1
    sed -i '/^* *[a-z]* *nofile *[0-9]*$/d' /etc/security/limits.conf
    sed -i  '$i *                soft    nofile          65535'  /etc/security/limits.conf
    sed -i  '$i *                hard    nofile          65535'  /etc/security/limits.conf
    for limits_file in `ls /etc/security/limits.d/[0-9]*-nproc.conf`
    do
      sed -i '/^* *[a-z]* *nproc *.*/d' $limits_file
      sed -i '/^root *[a-z]* *nproc *.*/d' $limits_file
      sed -i '$ a root        soft    nproc     unlimited' $limits_file
      sed -i '$ a root        hard    nproc     unlimited' $limits_file
      sed -i '$ a *          soft    nproc     137216' $limits_file
      sed -i '$ a *          hard    nproc     137216' $limits_file
    done
    
    ####禁用selinux####
    msg="${sysOptimize_process[3]}"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n30\n$msg\nXXX"
    echo 30
    sleep 1
    sed -i 's/^SELINUX=[a-zA-Z]*/SELINUX=disabled/' /etc/sysconfig/selinux
    sed -i 's/^SELINUX=[a-zA-Z]*/SELINUX=disabled/' /etc/selinux/config
    
    ####修改hosts文件####
    msg="${sysOptimize_process[4]}"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n40\n$msg\nXXX"
    echo 40
    sleep 1
    sed -i '/^127.0.0.1/d' /etc/hosts
    sed -i "/^$ipaddr *`hostname`$/d" /etc/hosts
    sed -i "1i $ipaddr   `hostname`" /etc/hosts
    
    ###更换yum源为阿里云####
    msg="${sysOptimize_process[5]}"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n50\n$msg\nXXX"
    echo 50
    if [ "$die_network" == "false" ];then
        if [ -f /etc/yum.repos.d/CentOS-Base.repo ];then
          /bin/mv -f /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
        fi
        if [ -f /etc/yum.repos.d/epel.repo ];then
          /bin/mv -f /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
        fi
        if [ -f /etc/yum.repos.d/epel-testing.repo ];then
          /bin/mv -f /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
        fi
    
        if [ "$system_ver" == "6" ];then
          curl -so /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo >>$logs_file 2>&1
          curl -so /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo >>$logs_file 2>&1
        elif [ "$system_ver" == "7" ];then
          curl -so /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo >>$logs_file 2>&1
          curl -so /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo >>$logs_file 2>&1
        elif [ "$system_ver" == "8" ];then
          curl -so /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo >>$logs_file 2>&1
          yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm >>$logs_file 2>&1
          sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
          sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
        fi
    else
        echo 60
        sleep 1
    fi
    
    ####修改时区####
    msg="${sysOptimize_process[6]}"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n70\n$msg\nXXX"
    echo 70
    sleep 1
    date -R |grep "+0800" >/dev/null 2>&1
    if [ "$?" != "0" ];then
        rm -rf /etc/localtime
        ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    fi
    
    if [ "$die_network" == "true" ];then
        echo -e "XXX\n80\n\nXXX"
        echo 80
        sleep 1
        echo -e "XXX\n90\n\nXXX"
        echo 90
        sleep 1
    else
        msg="${sysOptimize_process[7]}"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n80\n$msg\nXXX"
        echo 80
        yum -y install openssh-clients wget ntpdate unzip zip bzip2 net-tools gcc gcc-c++ glibc qrencode>>$logs_file 2>&1
        msg="${sysOptimize_process[8]}"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n90\n$msg\nXXX"
        echo 90
        ntpdate time.pool.aliyun.com >>$logs_file 2>&1
        hwclock --systohc
    fi
    
    echo -e "XXX\n100\nDone！！！\nXXX"
    echo 100
    sleep 1.30
    echo "">>$logs_file
    echo "">>$logs_file
    
    } | whiptail --backtitle "Author: Elvis" --title "${sysOptimize_process[9]}" --gauge " " 6 60 0

    whiptail --backtitle "Author: Elvis" --title "$prompt_message" --msgbox "${sysOptimize_process[10]}！！！" 10 70

}